#include "../include/Configuration.h"
#include <sstream>

#include "Poco/DateTime.h"
#include "Poco/DateTimeParser.h"

//#include "AppLogger.h"

using namespace Poco;
using namespace Poco::Util;
using namespace std;

/*create,
  consume,
  buy,
  sell,
  userReport,
  energyReport,
  profitReport,
  activityReport,
  accountReport,*/

Configuration::Configuration()
{
}

// Constructor that receive data stream as std::istream
Configuration::Configuration(std::istream& istr) : IniFileConfiguration(istr)
{
        Construct();
}

// Constructor that receive path as std::string
Configuration::Configuration(const std::string& path) : IniFileConfiguration(path)
{
        Construct();
}


void Configuration::Construct()
{

	    // Reserve an initial size for my events vector,
        // so he can allocate in advance. If allocate
		ostringstream numOfEvents;
		numOfEvents <<"numberOfEvents" ;
		int num = getInt(numOfEvents.str());
		events.reserve(num);


        int currConfFileIndex = 1;
        bool quit = false;
        while (!quit){
            try{
                ostringstream dateKeyNameBuilder;
                ostringstream typeKeyNameBuilder;
                ostringstream userIdKeyNameBuilder;
                ostringstream energyTypeKeyNameBuilder;
                ostringstream numOfUnitsKeyNameBuilder;
                ostringstream priceKeyNameBuilder;

                // Get event type
                dateKeyNameBuilder << "Type" << currConfFileIndex;
                string type = getString(typeKeyNameBuilder.str());

                if (type == "create")
                	readNcreateCreate();
                if (type == "consume")
                	readNcreateConsume();
                if (type == "sell")
                	readNcreateSell();
                if (type == "buy")
                	readNcreateBuy();
                if (type == "userReport")
                	readNcreateUserReport();
                if (type == "energyReport")
                	readNcreateEnergyReport();
                if (type == "accountReport")
                	readNcreateAccountReport();
                if (type == "profitReport")
                	readNcreateProfitReport();
                if (type == "activityReport")
                	readNcreateActivityReport();

                // 4. Increase event index
                currConfFileIndex++;
           }
           catch (NotFoundException ex)
           {
             quit = true;
           }
        }

}

CEventsConfiguration::MyEvent CEventsConfiguration::operator [] (int inIndex) const
{
        // Copy the contents of the requested my event
        // Note this is legal only in cases we have a copy constructor or we have a non pointer members.
        MyEvent result = *(mMyEvents[inIndex]);
        return result;
}

Configuration::~Configuration()
{
    vector<MyEvent*>::iterator iterator;

      // Deletes all MyEvents
  for(iterator = mMyEvents.begin();
              iterator != mMyEvents.end();
      iterator++)
      {
              if (*iterator != NULL)
              {
                      delete *iterator;
              }
      }
}

void Configuration::readNcreateCreate(){

	bool isValid = false;

	if (hasProperty("Date") & hasProperty("UserID") & hasProperty("EnergyType") & hasProperty("NumberOfUnits"))
		// Get event description
		 eventKeyNameBuilder << "event" << currConfFileIndex;
		 string desc = getString(eventKeyNameBuilder.str());


	// Parse the date
	int timeZoneDiff;
	DateTime date;
	if (!DateTimeParser::tryParse("%d/%n/%Y", dateStr, date, timeZoneDiff)){
	 // Failed to parse date. quit.
		quit = true;
	 	continue;
	}
}
void Configuration::readNcreateConsume(){}
void Configuration::readNcreateSell(){}
void Configuration::readNcreateBuy(){}
void Configuration::readNcreateUserReport(){}
void Configuration::readNcreateActivityReport(){}
void Configuration::readNcreateProfitReport(){}
void Configuration::readNcreateEnergyReport(){}
void Configuration::readNcreateAccountReport(){}
